using System;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour
{
    public Rigidbody rig;
    public Animator ani;

    public BirdState State;
    public float Speed;
    public float SpeedMuti = 0.3f;
    public float Angle;

    private float choiceTimer;
    private static readonly int IdleHash = Animator.StringToHash("Idle");
    private static readonly int EatHash = Animator.StringToHash("Eat");
    private static readonly int JumpHash = Animator.StringToHash("Jump");
    private static readonly int WalkHash = Animator.StringToHash("Walk");
    private static readonly int RunHash = Animator.StringToHash("Run");
    private static readonly int FlyHash = Animator.StringToHash("Fly");

    private void Awake()
    {
        State = BirdState.Idle;
    }

    private void Update()
    {
        UpdateMove();
        
        if (choiceTimer <= 0)
        {
            var random = RandomUtil.GetRandom(0, 10);
            if (random >= 0 && random < 1)
            {
                Idle();
                choiceTimer += 1;
            }
            else if (random >= 1 && random < 2)
            {
                Eat();
                choiceTimer += 1.5f;
            }
            else if (random >= 2 && random < 3)
            {
                Jump();
                choiceTimer += 1.5f;
            }
            else if (random >= 3 && random <= 6)
            {
                Walk();
                choiceTimer += 4f;
            }
            else if (random >= 6 && random <= 8)
            {
                Run();
                choiceTimer += 3f;
            }
            else
            {
                Fly();
                choiceTimer += 3f;
            }
        }
        else
        {
            choiceTimer -= Time.deltaTime;
        }
    }

    private void UpdateMove()
    {
        if(Speed == 0) return;
        rig.velocity = transform.eulerAngles.y.ToVector3() * (Speed * SpeedMuti);
    }

    private void Idle()
    {
        State = BirdState.Idle;
        ResetAni();
        ani.SetTrigger(IdleHash);
        Speed = 0;
        rig.velocity = Vector3.zero;
    }
    
    private void Eat()
    {
        State = BirdState.Eat;
        ResetAni();
        ani.SetTrigger(EatHash);
        Speed = 0;
        rig.velocity = Vector3.zero;
    }
    
    private void Jump()
    {
        State = BirdState.Jump;
        ResetAni();
        ani.SetTrigger(JumpHash);
        Speed = 0;
        rig.velocity = Vector3.zero;
    }
    
    private void Walk()
    {
        State = BirdState.Walk;
        ResetAni();
        ani.SetTrigger(WalkHash);
        Speed = 1;
        var angle = RandomUtil.GetRandom(0, 360);
        while (Math.Abs(angle - Angle) <= 90)
        {
            angle = RandomUtil.GetRandom(0, 360);
        }
        Angle = angle;
        rig.velocity = Angle.ToVector3() * (Speed * SpeedMuti);
        transform.eulerAngles = new Vector3(0, Angle, 0);
    }
    
    private void Run()
    {
        State = BirdState.Run;
        ResetAni();
        ani.SetTrigger(RunHash);
        Speed = 2;
        var angle = RandomUtil.GetRandom(0, 360);
        while (Math.Abs(angle - Angle) <= 90)
        {
            angle = RandomUtil.GetRandom(0, 360);
        }
        Angle = angle;
        rig.velocity = Angle.ToVector3() * (Speed * SpeedMuti);
        transform.eulerAngles = new Vector3(0, Angle, 0);
    }
    
    private void Fly()
    {
        State = BirdState.Fly;
        ResetAni();
        ani.SetTrigger(FlyHash);
        Speed = 3;
        var angle = RandomUtil.GetRandom(0, 360);
        while (Math.Abs(angle - Angle) <= 90)
        {
            angle = RandomUtil.GetRandom(0, 360);
        }
        Angle = angle;
        rig.velocity = Angle.ToVector3() * (Speed * SpeedMuti);
        transform.eulerAngles = new Vector3(0, Angle, 0);
    }

    private void ResetAni()
    {
        ani.ResetTrigger(IdleHash);
        ani.ResetTrigger(EatHash);
        ani.ResetTrigger(JumpHash);
        ani.ResetTrigger(WalkHash);
        ani.ResetTrigger(RunHash);
        ani.ResetTrigger(FlyHash);
    }
    
    public SkinnedMeshRenderer skinnedMeshRenderer;
    public bool IsSign;
    
    public void Sign()
    {
        skinnedMeshRenderer.material = GameManager.In.CaptureMaterial;
        IsSign = true;
    }
}

public enum BirdState
{
    Idle,
    Eat,
    Jump,
    Walk,
    Run,
    Fly,
}
